/**
 * Copyright 2011 Mathieu Magnaudet
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package edu.enac.geoapp.gui.component;

import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.beans.PropertyVetoException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JTextArea;

import org.geotools.swing.JMapPane;

import edu.enac.geoapp.gui.custom.component.ImageButton;
import edu.enac.geoapp.gui.listener.DataScrollListener;
import edu.enac.geoapp.gui.listener.ViewListener;
import edu.enac.geoapp.model.View;
import edu.enac.geoapp.services.FileStore;

/**
 * Composant qui réunit les boutons permettant de sélectionner une vue
 * particulière. A chaque nouvelle sélection les écouteurs abonnés reçoivent une
 * notifaction.
 * 
 * @author mathieu
 * 
 */
@SuppressWarnings("serial")
public class ViewSelector extends JComponent {
	private final ImageButton pirepButton;
	private final ImageButton navButton;
	private final ImageButton temsiButton;
	private final ImageButton radarButton;
	private final ImageButton satIrButton;
	private final ImageButton satVisButton;
	private final ImageButton polygonButton;
	private ImageScroller scroller;
	private List<Activable> activablelist;
	private List<ViewListener> listeners;
	private JTextArea metaData;
	private View selectedView;
	private JMapPane pane;
	private TemsiInternalFrame myFrame;

	/**
	 * Constructeur qui initialise les différents composants
	 * 
	 * @param initView
	 *            La vue initiale qui sera sélectionnée
	 */
	public ViewSelector() {

		// initialize components
		listeners = new ArrayList<ViewListener>();
		pirepButton = new ImageButton("images/pirep_button.png");
		navButton = new ImageButton("images/nav_button_green.png",
				"images/nav_button.png");
		navButton.setRolloverEnabled(true);
		navButton.setRolloverIcon(new ImageIcon(
				"images/nav_button_rollover.png"));

		polygonButton = new ImageButton("images/polygon_green_button.png",
				"images/polygon_button.png");
		polygonButton.setRolloverEnabled(true);
		polygonButton.setRolloverIcon(new ImageIcon(
				"images/polygon_button_rollover.png"));

		temsiButton = new ImageButton("images/temsi_button_green.png",
				"images/temsi_button.png");
		temsiButton.setRolloverEnabled(true);
		temsiButton.setRolloverIcon(new ImageIcon(
				"images/temsi_button_rollover.png"));

		satIrButton = new ImageButton("images/sat_button_green.png",
				"images/sat_button.png");
		satIrButton.setRolloverEnabled(true);
		satIrButton.setRolloverIcon(new ImageIcon(
				"images/sat_button_rollover.png"));

		satVisButton = new ImageButton("images/sat_vis_button_green.png",
				"images/sat_vis_button.png");
		satVisButton.setRolloverEnabled(true);
		satVisButton.setRolloverIcon(new ImageIcon(
				"images/sat_vis_button_rollover.png"));

		radarButton = new ImageButton("images/radar_button_green.png",
				"images/radar_button.png");
		radarButton.setRolloverEnabled(true);
		radarButton.setRolloverIcon(new ImageIcon(
				"images/radar_button_rollover.png"));
		scroller = new ImageScroller();

		metaData = new JTextArea("Métadonnées:", 10, 1);
		metaData.setBorder(BorderFactory.createEtchedBorder());
		metaData.setSize(100, 50);
		metaData.setEditable(false);

		// add listeners to the components
		navButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent e) {
				activate(navButton);
				setView(View.NAV);
			}
		});

		polygonButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent e) {
				activate(polygonButton);
				setView(View.POLYGON);
			}
		});
		temsiButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent e) {
				if (selectedView != View.TEMSI) {
					activate(temsiButton);
					setView(View.TEMSI);
					createInternalFrame();
				}
			}
		});

		satIrButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent e) {
				activate(satIrButton);
				setView(View.SAT_IR);
			}
		});

		satVisButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent e) {
				activate(satVisButton);
				setView(View.SAT_VIS);
			}
		});

		radarButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent e) {
				activate(radarButton);
				setView(View.RADAR_MTO);
			}
		});

		// populate the list with the activable components
		activablelist = new ArrayList<Activable>();
		activablelist.add(navButton);
		activablelist.add(temsiButton);
		activablelist.add(satIrButton);
		activablelist.add(radarButton);
		activablelist.add(satVisButton);
		activablelist.add(polygonButton);

		// add the components to the container
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		navButton.setAlignmentX(Component.CENTER_ALIGNMENT);
		add(navButton);
		add(Box.createVerticalStrut(5));
		polygonButton.setAlignmentX(Component.CENTER_ALIGNMENT);
		add(polygonButton);
		add(Box.createVerticalStrut(5));
		temsiButton.setAlignmentX(Component.CENTER_ALIGNMENT);
		add(temsiButton);
		add(Box.createVerticalStrut(5));
		satIrButton.setAlignmentX(Component.CENTER_ALIGNMENT);
		add(satIrButton);
		add(Box.createVerticalStrut(5));
		satVisButton.setAlignmentX(Component.CENTER_ALIGNMENT);
		add(satVisButton);
		add(Box.createVerticalStrut(5));
		radarButton.setAlignmentX(Component.CENTER_ALIGNMENT);
		add(radarButton);
		add(Box.createVerticalStrut(10));
		scroller.setAlignmentX(Component.CENTER_ALIGNMENT);
		add(scroller);
		add(Box.createVerticalStrut(10));
		metaData.setAlignmentX(Component.CENTER_ALIGNMENT);
		add(metaData);
		add(Box.createVerticalStrut(5));
		pirepButton.setAlignmentX(Component.CENTER_ALIGNMENT);
		add(pirepButton);
		add(Box.createVerticalStrut(10));
		setView(View.NAV);
		activate(navButton);

	}

	private void createInternalFrame() {
		myFrame = new TemsiInternalFrame();
		myFrame.setPath(FileStore.getInstance().getPathToTemsi());
		myFrame.updateDoc();
		myFrame.setVisible(true); 
		pane.add(myFrame);
		try {
			myFrame.setSelected(true);
		} catch (java.beans.PropertyVetoException e) {

		}

	}

	private void setView(View view) {
		if (view != selectedView) {
			if (myFrame != null) {
				try {
					myFrame.setClosed(true);
					myFrame = null;
				} catch (PropertyVetoException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			this.selectedView = view;
			alertViewListeners(view);
		}
	}

	/**
	 * Active le composant passé en argument et désactive tous les autres
	 * 
	 * @param component
	 *            The component to activate
	 */
	private void activate(Activable component) {
		for (Activable a : activablelist) {
			if (a.equals(component))
				a.setActive(true);
			else
				a.setActive(false);
		}
	}

	/**
	 * ajouter un écouteur
	 * 
	 * @param l
	 */
	public void addViewListener(ViewListener l) {
		listeners.add(l);
	}

	/**
	 * supprimer un écouteur
	 * 
	 * @param l
	 */
	public void removeViewListener(ViewListener l) {
		listeners.remove(l);
	}

	private void alertViewListeners(View newView) {
		for (ViewListener l : listeners)
			l.viewChanged(newView);
	}

	/**
	 * @return the selectedView
	 */
	public View getView() {
		return selectedView;
	}

	public void addDataScrollListener(DataScrollListener listener) {
		scroller.addScrollerListener(listener);
	}

	public void setEnabledNextButton(boolean enabled) {
		scroller.setEnabledNextButton(enabled);
	}

	public void setEnabledPreviousButton(boolean enabled) {
		scroller.setEnabledPreviousButton(enabled);
	}

	public void setMetaData(String text) {
		metaData.setText(text);
	}

	public void appendData(String text) {
		metaData.append(text);
	}

	public void addPane(JMapPane pane) {
		this.pane = pane;
	}
}